package field.objects;

import java.util.List;

import util.Constants;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GLTexture;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class PlayerShip extends Ship implements Constants {
	private int score;
	private int life;
	private Sprite shipThruster;
	private List<Weapon> weaponList;
	private static long timeSpan = 0;
	private ParticleEffect effect;

	// private ParticleEffectPool effectPool;
	public PlayerShip() {
		super(new Texture("data/ship/Spaceship.png"));
		load();
	}

	public PlayerShip(List<Weapon> weaponList) {
		super(new Texture("data/ship/Spaceship.png"));
		load();
		this.weaponList = weaponList;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}

	@Override
	public void render(SpriteBatch batch) {
		initShip(batch);
		timeSpan++;
		renderBullet(batch);
		renderThruster(batch, Gdx.graphics.getDeltaTime());
		// renderThr(batch);
		draw(batch);
	}

	@Override
	public void load() {
		getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		// Resizing the image to %75 from original (82,121)
		setSize(62, 91);
		setPosition(WIDTH / 2 - getWidth() / 2, 0);
		setSpeed(7f);
	}

	private void initShip(SpriteBatch batch) {
		if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
			if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
				if (getX() > 0)
					translateX(-this.getSpeed() * 2);
			} else {
				if (getX() > 0)
					translateX(-this.getSpeed());
			}
		}
		if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
				if (getX() < WIDTH - getWidth())
					translateX(this.getSpeed() * 2);
			} else {
				if (getX() < WIDTH - getWidth())
					translateX(this.getSpeed());
			}
		}
		if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
			if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
				if (getY() < HEIGHT - getHeight()) {
					translateY(this.getSpeed() * 2);
					if (getY() != 0) {
						loadThr();
						renderThr(batch);
					}
				}
			} else {
				if (getY() < HEIGHT - getHeight()) {
					translateY(this.getSpeed());
					if (getY() != 0) {
						// this.effect = new ParticleEffect();
						// loadThruster();
						// renderThruster(batch, Gdx.graphics.getDeltaTime());
						loadThr();
						renderThr(batch);
					}
				}
			}
		}
		if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
			if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
				if (getY() > 0)
					translateY(-this.getSpeed() * 2);
			} else {
				if (getY() > 0)
					translateY(-this.getSpeed());
			}
		}
		if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
			equipWeapon();
			timeSpan = 0;
		}
	}

	private void equipWeapon() {
		if (weaponList != null && !weaponList.isEmpty()) {
			Weapon lastBullet = weaponList.get(weaponList.size() - 1);
			// System.out.println(timeSpan + " " + lastBullet.getRapidity() +
			// " "
			// + Gdx.graphics.getDeltaTime());
			// if (lastBullet.getY() < lastBullet.getWidth() *
			if (timeSpan < lastBullet.getRapidity()) {
				return;
			}
		}
		Weapon w1 = new Laser();
		w1.setPosition(getX(), getY() + 30);
		w1.setSize(8, 15);
		Weapon w2 = new Laser();
		w2.setPosition(getX() + getWidth() - 8, getY() + 30);
		w2.setSize(8, 15);
		weaponList.add(w1);
		weaponList.add(w2);
		weaponList.get(weaponList.size() - 1).loadSound().play();
	}

	public void renderBullet(SpriteBatch batch) {
		if (!weaponList.isEmpty()) {
			for (int i = 0; i < weaponList.size(); i++) {
				weaponList.get(i).translateY(weaponList.get(i).getAtkSpeed());
				weaponList.get(i).draw(batch);
				if (weaponList.get(i).getY() > 900) {
					weaponList.remove(i);
					i--;
				}
			}
		}
	}

	private void loadThr() {
		GLTexture.setEnforcePotImages(false);
		Texture shipThrusterTexture = new Texture("data/ship/thruster.png");
		shipThrusterTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		shipThruster = new Sprite(shipThrusterTexture);
		shipThruster.setSize(7, 18);
	}

	private void renderThr(SpriteBatch batch) {
		if (shipThruster != null) {
			shipThruster.setPosition(getX() + 17.5f, getY() - 15);
			shipThruster.draw(batch);
			shipThruster.setPosition(getX() + getWidth() - 24.5f, getY() - 15);
			shipThruster.draw(batch);
		}
	}

	@Override
	public void dispose() {
		if (getTexture() != null) {
			getTexture().dispose();
		}
		if (effect != null) {
			effect.dispose();
		}
		// effectPool.clear();
		if (shipThruster != null) {
			shipThruster.getTexture().dispose();
		}
	}

	public void loadThruster() {
		this.effect = new ParticleEffect();
		effect.load(Gdx.files.internal("data/effects/thruster.p"), Gdx.files.internal("data/ship"));
		effect.setPosition(getX() + 17.5f, getY() - 15);
		// effect.getEmitters().get(0).allowCompletion();
		// System.out.println(getX() + " " + getY());
		effect.setDuration(10);
		// ScaleParticle(effect, .5f);
		effect.start();
		// effectPool = new ParticleEffectPool(effect, 1, 1);
		// effect = effectPool.obtain();
	}

	public void renderThruster(SpriteBatch batch, float delta) {
		if (effect != null) {
			effect.draw(batch, delta);
		}
	}

	public void ScaleParticle(ParticleEffect p, float scale) {
		p.getEmitters().get(0).getScale().setHigh(p.getEmitters().get(0).getScale().getHighMax() * scale);
		p.getEmitters().get(0).getScale().setLow(p.getEmitters().get(0).getScale().getLowMax() * scale);
		p.getEmitters().get(0).getVelocity().setHighMax(p.getEmitters().get(0).getVelocity().getHighMax() * scale);
		p.getEmitters().get(0).getVelocity().setHighMin(p.getEmitters().get(0).getVelocity().getHighMin() * scale);
		p.getEmitters().get(0).getVelocity().setLow(p.getEmitters().get(0).getVelocity().getLowMax() * scale);
		p.getEmitters().get(0).setMaxParticleCount((int) (p.getEmitters().get(0).getMaxParticleCount() * scale));
		p.getEmitters().get(0).setMinParticleCount((int) (p.getEmitters().get(0).getMinParticleCount() * scale));
	}
}
